package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerArrayEvent;
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.CorpModel;
	import com.micmac.customer.model.OrgModel;
	import com.micmac.customer.view.OrganisationView;
	import com.micmac.customer.vo.OrgProcessVO;
	import com.micmac.customer.vo.OrgVO;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.collections.IList;
	import mx.events.IndexChangedEvent;
	
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;

	public class OrganisationViewMediator extends ModuleMediator
	{
		[Inject]
		public var organisationView:OrganisationView;
		[Inject]
		public var customerService:ICustomerService;
		[Inject]
		public var dataModel:OrgModel;
		
		[Bindable]
		private var orgList:ArrayCollection = new ArrayCollection();
		
		/*[Bindable]
		private var orgListIndex:int = 0;
		[Bindable]
		private var totalListIndex:int = 0;*/
		
		[Bindable]
		private var _orgListIndex:int = 0;
		[Bindable]
		private var _orgProcessListIndex:int = 0;
		
		override public function onRegister():void
		{
			customerService.getRegions();
			addViewListener(OrganisationView.ORG_SAVE, onOrgSave);
			addViewListener(OrganisationView.ORG_PROCESS_SAVE, onOrgProcessSave)
			
			addViewListener(OrganisationView.ORG_PREV, OnOrgPrev);
			addViewListener(OrganisationView.ORG_NEXT, onOrgNext);
			addViewListener(OrganisationView.ORG_PROCESS_NEXT, onOrgProcessNext);
			addViewListener(OrganisationView.ORG_PROCESS_PREV, OnOrgProcessPrev);
			
			addViewListener(OrganisationView.STACK_CHANGED, onStackChanged);
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.ORG_SAVED,onOrgSaved);
			eventMap.mapListener(eventDispatcher,CustomerEvent.ORG_PROCESS_SAVED,onOrgProcesSaved);
			
		}
		
		private function OnOrgProcessPrev(event:Event):void
		{
			_orgProcessListIndex--;
			organisationView.btnNext.enabled = true;
			organisationView.btnSave.enabled = false;
			
			organisationView.txtOrgProcess.text = dataModel.orgProcessList[_orgProcessListIndex].orgProces;
			organisationView.cmbOrg.textInput.text =dataModel.orgProcessList[_orgProcessListIndex].org;
			organisationView.cmbProcess.textInput.text = dataModel.orgProcessList[_orgProcessListIndex].process;
			organisationView.cmbOrgPrcsStatus.textInput.text = dataModel.orgProcessList[_orgProcessListIndex].status;
			if(_orgProcessListIndex  == 0)
			{
				organisationView.btnPrev.enabled = false;
			}
		}

		private function onOrgProcessNext(event:Event):void
		{
			_orgProcessListIndex++;;
			organisationView.btnPrev.enabled = true;
			organisationView.btnSave.enabled = false;
			
			organisationView.txtOrgProcess.text = dataModel.orgProcessList[_orgProcessListIndex].orgProces;
			organisationView.cmbOrg.textInput.text = dataModel.orgProcessList[_orgProcessListIndex].org;
			organisationView.cmbProcess.textInput.text = dataModel.orgProcessList[_orgProcessListIndex].process;
			organisationView.cmbOrgPrcsStatus.textInput.text = dataModel.orgProcessList[_orgProcessListIndex].status;
			if(_orgProcessListIndex  == dataModel.orgProcessList.length - 1)
			{
				organisationView.btnNext.enabled = false;
			}
		}
		
		private function onOrgNext(event:Event):void
		{
			_orgListIndex++;
			organisationView.btnPrev.enabled = true;
			organisationView.btnSave.enabled = false;
			
			organisationView.orgNameText.text = dataModel.orgList[_orgListIndex].orgName;
			organisationView.orgDescText.text =dataModel.orgList[_orgListIndex].orgDescription;
			organisationView.orgRegionCombo.textInput.text = dataModel.orgList[_orgListIndex].region;
			organisationView.orgStatusText.text = dataModel.orgList[_orgListIndex].status;
			if(_orgListIndex  == dataModel.orgList.length - 1)
			{
				organisationView.btnNext.enabled = false;
			}
		}
		private function OnOrgPrev(event:Event):void
		{
			_orgListIndex--;
			organisationView.btnNext.enabled = true;
			organisationView.btnSave.enabled = false;
			
			organisationView.orgNameText.text = dataModel.orgList[_orgListIndex].orgName;
			organisationView.orgDescText.text =dataModel.orgList[_orgListIndex].orgDescription;
			organisationView.orgRegionCombo.textInput.text = dataModel.orgList[_orgListIndex].region;
			organisationView.orgStatusText.text = dataModel.orgList[_orgListIndex].status;
			if(_orgListIndex  == 0)
			{
				organisationView.btnPrev.enabled = false;
			}
		}
		private function onStackChanged(event:Event):void
		{
			switch(organisationView.vsOrg.selectedIndex)
			{
				case 0:
				{
					onOrgSaved();
					organisationView.btnSave.enabled = true;
					organisationView.btnAdd.enabled = false;
					break;
				}
				case 1:
				{
					onOrgProcesSaved();
					organisationView.btnSave.enabled = true;
					organisationView.btnAdd.enabled = false;
					break;
				}
				
			}
		}

		
		private function onOrgProcesSaved(event:CustomerEvent = null):void
		{
			if(dataModel.orgProcessList.length != 1 && dataModel.orgProcessList.length != 0)
			{
				organisationView.btnPrev.enabled = true;
			}else
			{
				organisationView.btnPrev.enabled = false;
			}
			organisationView.btnAdd.enabled = true;
			organisationView.btnSave.enabled = false;
			organisationView.btnNext.enabled = false;
			_orgProcessListIndex = dataModel.orgProcessList.length - 1;
			organisationView.btnAdd.label = '+OrgProcess';
		}

		
		
		private function onOrgSaved(event:CustomerEvent = null):void
		{
			if(dataModel.orgList.length != 1 && dataModel.orgList.length != 0)
			{
				organisationView.btnPrev.enabled = true;
			}else
			{
				organisationView.btnPrev.enabled = false;
			}
			organisationView.btnAdd.enabled = true;
			organisationView.btnSave.enabled = false;
			organisationView.btnNext.enabled = false;
			_orgListIndex = dataModel.orgList.length - 1;
			organisationView.btnAdd.label = '+Org';

		}
		private function onOrgProcessSave(event:Event):void
		{
			var orgProcessVO:OrgProcessVO = new OrgProcessVO();
			orgProcessVO.orgProces = organisationView.txtOrgProcess.text;
			orgProcessVO.org = organisationView.cmbOrg.textInput.text;
			orgProcessVO.process = organisationView.cmbProcess.textInput.text;
			orgProcessVO.status = organisationView.cmbOrgPrcsStatus.textInput.text;
			customerService.saveOrgProcess(orgProcessVO);
		}

		private function onOrgSave(event:Event):void
		{
			var orgVo:OrgVO = new OrgVO();
			orgVo.orgName = organisationView.orgNameText.text;
			orgVo.orgDescription = organisationView.orgDescText.text;
			orgVo.region = organisationView.orgRegionCombo.textInput.text
			orgVo.status = organisationView.orgStatusText.text;
			customerService.saveOrg(orgVo);
			
		}
		
		private function onOrgAdd(event:Event):void
		{
			organisationView.orgNameText.text = '';
			organisationView.orgDescText.text = '';
			organisationView.orgRegionCombo.selectedItem = '';
			organisationView.orgStatusText.text = '';
		}
		
		private function onSetRegion(e:CustomerArrayEvent):void
		{
			var regionList:ArrayCollection = e.listdata;
			//var arrList:ArrayList = new ArrayList(regionList);
			organisationView.orgRegionCombo.dataProvider = regionList;
		}
	}
}